﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.SqlClient;
using Entities;

namespace DataAccessLayer
{
    public class LopHocDAL : DBConnection
    {

        public List<LopHocEntity> getAll()
        {
            List<LopHocEntity> lst = new List<LopHocEntity>();
            try
            {
                MySqlCommand cmd = new MySqlCommand("lop_hoc_getAll", con);
                cmd.CommandType = CommandType.StoredProcedure;

                if (con.State != ConnectionState.Open)
                    con.Open();
                MySqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                while (dr.Read())
                {
                    LopHocEntity obj = new LopHocEntity();
                    obj.MaLopHoc = Convert.ToInt32(dr["MaLopHoc"]);
                    obj.TenLopHoc = Convert.ToString(dr["TenLopHoc"]);
                    obj.MaVuDieu = Convert.ToInt32(dr["MaVuDieu"]);
                    obj.MoTa = Convert.ToString(dr["MoTa"]);
                    lst.Add(obj);
                }
                dr.Close();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
            return lst;
        }

        public LopHocEntity getById(int _id)
        {
            LopHocEntity obj = new LopHocEntity();
            try
            {
                MySqlCommand cmd = new MySqlCommand("lop_hoc_getById", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("maLopHoc", _id);

                if (con.State != ConnectionState.Open)
                    con.Open();
                MySqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                if (dr.Read())
                {
                    obj.MaLopHoc = Convert.ToInt32(dr["MaLopHoc"]);
                    obj.TenLopHoc = Convert.ToString(dr["TenLopHoc"]);
                    obj.MaVuDieu = Convert.ToInt32(dr["MaVuDieu"]);
                    obj.MoTa = Convert.ToString(dr["MoTa"]);
                }
                dr.Close();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
            return obj;
        }

        public List<LopHocEntity> getByVuDieuId(int _id)
        {
            List<LopHocEntity> lst = new List<LopHocEntity>();
            try
            {
                MySqlCommand cmd = new MySqlCommand("lop_hoc_getByVuDieuId", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("maVuDieu", _id);

                if (con.State != ConnectionState.Open)
                    con.Open();
                MySqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                while (dr.Read())
                {
                    LopHocEntity obj = new LopHocEntity();
                    obj.MaLopHoc = Convert.ToInt32(dr["MaLopHoc"]);
                    obj.TenLopHoc = Convert.ToString(dr["TenLopHoc"]);
                    obj.MaVuDieu = Convert.ToInt32(dr["MaVuDieu"]);
                    obj.MoTa = Convert.ToString(dr["MoTa"]);
                    lst.Add(obj);
                }
                dr.Close();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
            return lst;
        }

        public int addLopHoc(LopHocEntity _obj)
        {
            int result = 0;
            try
            {
                if (_obj.MaLopHoc != -1)
                    throw new Exception("Lop Hoc Da Ton Tai");

                MySqlCommand cmd = new MySqlCommand("lop_hoc_insert", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("tenLopHoc", _obj.TenLopHoc);
                cmd.Parameters.AddWithValue("maVuDieu", _obj.MaVuDieu);
                cmd.Parameters.AddWithValue("moTa", _obj.MoTa);
                cmd.Parameters.AddWithValue("lastId", MySqlDbType.Int32);
                cmd.Parameters["lastId"].Direction = ParameterDirection.Output;

                if (con.State != ConnectionState.Open)
                    con.Open();
                result = cmd.ExecuteNonQuery();
                int i = Convert.ToInt32(cmd.Parameters["lastId"].Value);
                if (_obj.MaLopHoc == -1)
                    _obj.MaLopHoc = i;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
            return result;
        }

        public int deleteLopHoc(int _id)
        {
            int result = 0;
            try
            {
                if (_id == -1)
                    return 0;
                MySqlCommand cmd = new MySqlCommand("lop_hoc_delete", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("maLopHoc", _id);
                cmd.Parameters["maLopHoc"].Direction = ParameterDirection.Input;

                if (con.State != ConnectionState.Open)
                    con.Open();
                result = cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
            return result;
        }

        public int updateLopHoc(LopHocEntity _obj)
        {
            int result = 0;
            try
            {
                if (_obj.MaLopHoc == -1)
                    return addLopHoc(_obj);

                MySqlCommand cmd = new MySqlCommand("lop_hoc_update", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("maLopHoc", _obj.MaLopHoc);
                cmd.Parameters.AddWithValue("tenLopHoc", _obj.TenLopHoc);
                cmd.Parameters.AddWithValue("maVuDieu", _obj.MaVuDieu);
                cmd.Parameters.AddWithValue("moTa", _obj.MoTa);

                if (con.State != ConnectionState.Open)
                    con.Open();
                result = cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
            return result;
        }
    }
}
